package com.jiangyg.mall.core.support.restful;

import com.jiangyg.mall.core.utils.JsonUtils;
import com.jiangyg.mall.core.utils.bean.BeanUtils;
import org.apache.commons.lang.StringUtils;

import java.io.Serializable;
import java.util.Locale;
import java.util.ResourceBundle;

/**
 * 类描述：返回数据
 *
 * @author jiangyg
 * @date 2021-10-07
 */
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 7659732641132231672L;

    /**
     * 状态码资源文件
     */
    public static final ResourceBundle bundle = ResourceBundle.getBundle("i18n/code", Locale.getDefault());

    /**
     * 请求成功状态码
     */
    private final static String SUCCESS_CODE = "200";

    /**
     * 请求失败状态码
     */
    private final static String FAIL_CODE = "500";

    /**
     * 返回状态码
     */
    private String code;

    /**
     * 返回消息
     */
    private String msg;

    /**
     * 返回数据
     */
    private T body;

    public Result() {
    }

    private Result(String code) {
        this.code = code;
        this.msg = bundle.getString(code);
    }

    private Result(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public Result(String code, String msg, T body) {
        this.code = code;
        this.msg = msg;
        this.body = body;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        if (StringUtils.isBlank(this.msg)) {
            this.msg = bundle.getString(this.code);
        }
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getBody() {
        return body;
    }

    public void setBody(T body) {
        this.body = body;
    }

    /**
     * 功能描述：生成请求成功数据
     *
     * @return 结果
     */
    public static <T> Result<T> ok() {
        return new Result<>(SUCCESS_CODE);
    }

    /**
     * 功能描述：生成请求成功数据
     *
     * @param body 返回结果
     * @param <T>  泛型
     * @return 结果
     */
    public static <T> Result<T> ok(T body) {
        final Result<T> result = ok();
        result.setBody(body);
        return result;
    }

    /**
     * 功能描述：判断是否成功
     *
     * @return 是否
     */
    public boolean success() {
        return StringUtils.equals(this.code, SUCCESS_CODE);
    }

    /**
     * 功能描述：实例化指定状态码的结果
     *
     * @param code 状态码
     * @return 结果
     */
    public static <T> Result<T> instance(String code) {
        return new Result<>(code);
    }

    /**
     * 功能描述：实例化指定状态码的结果
     *
     * @param code 状态码
     * @param body 结果
     * @param <T>  泛型
     * @return 结果
     */
    public static <T> Result<T> instance(String code, T body) {
        final Result<T> instance = instance(code);
        instance.setBody(body);
        return instance;
    }

    /**
     * 功能描述：生成通用的系统异常信息
     *
     * @return 结果
     */
    public static <T> Result<T> error() {
        return new Result<>(FAIL_CODE);
    }

    /**
     * 功能描述：生成通用的系统异常信息
     *
     * @param msg 指定异常信息
     * @return 结果
     */
    public static <T> Result<T> error(String msg) {
        final Result<T> error = error();
        error.setMsg(msg);
        return error;
    }

    public static <T> Result<T> saveOrUpdate(boolean success) {
        final Result<T> result = new Result<>();
        if (success) {
            result.setMsg("保存成功");
            result.setCode(SUCCESS_CODE);
        } else {
            result.setMsg("保存失败");
            result.setCode(FAIL_CODE);
        }
        return result;
    }

    public static <T> Result<T> delete(boolean success) {
        final Result<T> result = new Result<>();
        if (success) {
            result.setMsg("删除成功");
            result.setCode(SUCCESS_CODE);
        } else {
            result.setMsg("删除失败");
            result.setCode(FAIL_CODE);
        }
        return result;
    }

    public String toJSONString() {
        return JsonUtils.toJSONString(this);
    }

    @Override
    public String toString() {
        return BeanUtils.toString(this);
    }

}
